package vip.zhenzicheng.nio.nio;

import vip.zhenzicheng.nio.Const;

import java.io.IOException;
import java.util.Scanner;

/**
 * NIO 通信客户端
 *
 * @author zhenzicheng
 * @date: 2022-06-01 18:22
 */
public class NioClient {
  private static NioClientHandle nioClientHandle;

  public static void start() {
    nioClientHandle = new NioClientHandle(Const.DEFAULT_SERVER_IP, Const.DEFAULT_PORT);
    new Thread(nioClientHandle, "client").start();
  }

  // 向服务器发送消息
  public static boolean sendMsg(String msg) throws IOException {
    nioClientHandle.sendMsg(msg);
    return true;
  }

  public static void main(String[] args) throws IOException {
    start();
    try (Scanner scanner = new Scanner(System.in)) {
      while (NioClient.sendMsg(scanner.next())) ;
    }
  }
}
